package com.tian.arch.util.divider

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * RecycleView的分割线
 */
class RecycleViewDivider(
    context: Context,
    orientation: Int,
    dividerHeight: Int,
    dividerColor: Int
) : RecyclerView.ItemDecoration() {

    private val mPaint: Paint
    private var mDividerHeight = dividerHeight // 分割线高度，默认为1px
    private var mOrientation = orientation
    var marLeftAndRight = 0

    init {
        require(!(orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL)) { "请输入正确的参数！" }
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint.color = dividerColor
        mPaint.style = Paint.Style.FILL
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.set(0, 0, 0, mDividerHeight)
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawVertical(c, parent)
        } else {
            drawHorizontal(c, parent)
        }
    }

    //绘制横向 item 分割线
    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft //获取分割线的左边距，即RecyclerView的padding值
        val right = parent.measuredWidth - parent.paddingRight //分割线右边距
        val childSize = if (parent.childCount > 0) parent.childCount - 1 else parent.childCount
        //遍历所有item view，为它们的下方绘制分割线
        for (i in 0 until childSize) {
            val child: View = parent.getChildAt(i)
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams
            val top: Int = child.bottom + layoutParams.bottomMargin
            val bottom = top + mDividerHeight

            mPaint.let {
                canvas.drawRect(
                    left.toFloat() + marLeftAndRight,
                    top.toFloat(),
                    right.toFloat() - marLeftAndRight,
                    bottom.toFloat(),
                    it
                )

            }
        }
    }

    //绘制纵向 item 分割线
    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop
        val bottom = parent.measuredHeight - parent.paddingBottom
        val childSize = if (parent.childCount > 0) parent.childCount - 1 else parent.childCount
        for (i in 0 until childSize) {
            val child: View = parent.getChildAt(i)
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams
            val left: Int = child.right + layoutParams.rightMargin
            val right = left + mDividerHeight

            mPaint.let {
                canvas.drawRect(
                    left.toFloat(),
                    top.toFloat(),
                    right.toFloat(),
                    bottom.toFloat(),
                    it
                )
            }
        }
    }

}